package com.ias.common.utils.log;

import java.io.IOException;
import java.sql.SQLException;

/**
 * ExcpUtil 使用示例
 * 演示各种异常处理功能
 */
public class ExcpUtilExample {
    
    public static void main(String[] args) {
        System.out.println("=== ExcpUtil 功能演示 ===\n");
        
        // 1. 基本堆栈信息获取
        demonstrateBasicStackTrace();
        
        // 2. 限制深度的堆栈信息
        demonstrateStackTraceWithDepth();
        
        // 3. 简要堆栈信息（JSON格式）
        demonstrateSubStackTrace();
        
        // 4. 异常链处理
        demonstrateExceptionChain();
        
        // 5. 异常查找功能
        demonstrateExceptionSearch();
        
        // 6. 安全消息获取
        demonstrateSafeMessage();
        
        // 7. 格式化错误消息
        demonstrateFormattedError();
    }
    
    /**
     * 演示基本堆栈信息获取
     */
    private static void demonstrateBasicStackTrace() {
        System.out.println("1. 基本堆栈信息获取:");
        try {
            throwSimpleException();
        } catch (Exception e) {
            String stackTrace = ExcpUtil.getStackTraceString(e);
            System.out.println(stackTrace);
        }
        System.out.println();
    }
    
    /**
     * 演示限制深度的堆栈信息
     */
    private static void demonstrateStackTraceWithDepth() {
        System.out.println("2. 限制深度的堆栈信息（最多5层）:");
        try {
            deepMethodCall1();
        } catch (Exception e) {
            String stackTrace = ExcpUtil.getStackTraceString(e, 5);
            System.out.println(stackTrace);
        }
        System.out.println();
    }
    
    /**
     * 演示简要堆栈信息（JSON格式）
     */
    private static void demonstrateSubStackTrace() {
        System.out.println("3. 简要堆栈信息（JSON格式）:");
        try {
            throwSimpleException();
        } catch (Exception e) {
            String subTrace = ExcpUtil.getSubStackTrace(e, 3);
            System.out.println(subTrace);
        }
        System.out.println();
    }
    
    /**
     * 演示异常链处理
     */
    private static void demonstrateExceptionChain() {
        System.out.println("4. 异常链处理:");
        try {
            throwChainedException();
        } catch (Exception e) {
            System.out.println("原始异常: " + e.getClass().getSimpleName() + " - " + e.getMessage());
            
            Throwable rootCause = ExcpUtil.getRootCause(e);
            System.out.println("根本原因: " + rootCause.getClass().getSimpleName() + " - " + rootCause.getMessage());
            
            String rootMessage = ExcpUtil.getRootCauseMessage(e);
            System.out.println("根本原因消息: " + rootMessage);
        }
        System.out.println();
    }
    
    /**
     * 演示异常查找功能
     */
    private static void demonstrateExceptionSearch() {
        System.out.println("5. 异常查找功能:");
        try {
            throwChainedException();
        } catch (Exception e) {
            // 检查是否包含特定类型的异常
            boolean containsIOException = ExcpUtil.containsException(e, IOException.class);
            boolean containsSQLException = ExcpUtil.containsException(e, SQLException.class);
            
            System.out.println("包含 IOException: " + containsIOException);
            System.out.println("包含 SQLException: " + containsSQLException);
            
            // 查找特定类型的异常
            IOException ioException = ExcpUtil.findException(e, IOException.class);
            if (ioException != null) {
                System.out.println("找到的 IOException: " + ioException.getMessage());
            }
        }
        System.out.println();
    }
    
    /**
     * 演示安全消息获取
     */
    private static void demonstrateSafeMessage() {
        System.out.println("6. 安全消息获取:");
        
        // 正常异常
        Exception normalEx = new RuntimeException("这是一个正常的异常消息");
        System.out.println("正常异常消息: " + ExcpUtil.safeGetMessage(normalEx));
        
        // 空消息异常
        Exception emptyEx = new RuntimeException();
        System.out.println("空消息异常: " + ExcpUtil.safeGetMessage(emptyEx));
        
        // null异常
        System.out.println("null异常: " + ExcpUtil.safeGetMessage(null));
        System.out.println();
    }
    
    /**
     * 演示格式化错误消息
     */
    private static void demonstrateFormattedError() {
        System.out.println("7. 格式化错误消息:");
        try {
            throwSimpleException();
        } catch (Exception e) {
            String formattedError = ExcpUtil.buildErrorMessage(e);
            System.out.println(formattedError);
        }
        System.out.println();
    }
    
    // 辅助方法 - 抛出简单异常
    private static void throwSimpleException() throws Exception {
        throw new RuntimeException("这是一个测试异常");
    }
    
    // 辅助方法 - 创建深层调用栈
    private static void deepMethodCall1() throws Exception {
        deepMethodCall2();
    }
    
    private static void deepMethodCall2() throws Exception {
        deepMethodCall3();
    }
    
    private static void deepMethodCall3() throws Exception {
        deepMethodCall4();
    }
    
    private static void deepMethodCall4() throws Exception {
        deepMethodCall5();
    }
    
    private static void deepMethodCall5() throws Exception {
        throw new RuntimeException("深层调用异常");
    }
    
    // 辅助方法 - 抛出异常链
    private static void throwChainedException() throws Exception {
        try {
            try {
                throw new IOException("IO操作失败");
            } catch (IOException e) {
                throw new SQLException("数据库连接失败", e);
            }
        } catch (SQLException e) {
            throw new RuntimeException("业务处理失败", e);
        }
    }
} 